package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.bean.LinkBean;
import com.project.service.ILinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("link")
public class LinkController {
    @Autowired
    private ILinkService service;

    @RequestMapping("findAll")
    public void findAll(String pageNum,HttpServletResponse response)throws Exception{
            response.setContentType("text/html;charset=utf-8");
            if(pageNum == null){
                PageInfo<LinkBean> lists =service.cutAll(1);
                String str ="<table border='1' width='60%'>";
                str +="<thead><th>姓名</th><th>生日</th><th>电话</th><th>操作</th></thead>";
                str += "<tbody>";
                for(LinkBean link:lists.getList()){
                    str += "<tr><td>"+link.getName()+"</td><td>"+link.getBirthday()+"</td>" +
                            "<td>"+link.getPhone()+"</td><td><a href='/link/del?id="+link.getId()+"'>删除</a>"+" "+"<a href='/link/findById?id="+link.getId()+"'>修改</a></td></tr>";
                }
                str +="</tbody></table>";
                str += "<a href='/html/addLink.html'>添加</a><br>";
                for(int i=1;i<=lists.getSize();i++){
                    str +="<a href='cutAll?pageNum="+i+"'>"+i+"</a> ";
                }
                response.getWriter().print(str);
            }
    }
    @RequestMapping("/add")
    public String add(LinkBean link){
        service.add(link);
        return "redirect:/link/findAll";
    }
    @RequestMapping("del")
    public String del(int id){
        service.delId(id);
        return "redirect:/link/findAll";
    }
    @RequestMapping("findById")
    public void  findById(int id,HttpServletResponse resp)throws Exception{
        resp.setContentType("text/html;charset=utf-8");
         LinkBean linkBean = service.findById(id);
         String str = "<form action='/link/update'>";
          str +="<input type='hidden' name='id' value='"+id+"'>";
          str +="姓名:"+linkBean.getName()+"<br>";
          str +="生日:"+linkBean.getBirthday()+"<br>";
          str +="电话:<input type='text' name='phone' value='"+linkBean.getPhone()+"'>";
          str +="<input type='submit' value='修改'>";
         str +="</form";
        resp.getWriter().print(str);
    }
    @RequestMapping("update")
    public String update(int id,String phone){
        service.update(id,phone);
        return "redirect:/link/findAll";
    }

    @RequestMapping("cutAll")
    public void cutAll(int pageNum,HttpServletResponse resp)throws Exception{
        resp.setContentType("text/html;charset=utf-8");
        PageInfo<LinkBean> list =service.cutAll(pageNum);
        String strs ="<table border='1' width='60%'>";
        strs +="<thead><th>姓名</th><th>生日</th><th>电话</th><th>操作</th></thead>";
        strs += "<tbody>";
        for(LinkBean link:list.getList()){
            strs += "<tr><td>"+link.getName()+"</td><td>"+link.getBirthday()+"</td>" +
                    "<td>"+link.getPhone()+"</td><td><a href='/link/del?id="+link.getId()+"'>删除</a>"+" "+"<a href='/link/findById?id="+link.getId()+"'>修改</a></td></tr>";
        }
        strs +="</tbody></table>";
        strs += "<a href='/html/addLink.html'>添加</a><br>";
        for(int i=1;i<=list.getSize();i++){
            strs +="<a href='cutAll?pageNum="+i+"'>"+i+"</a> ";
        }
        resp.getWriter().print(strs);
    }
}
